﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MyERP.Models;

namespace MyERP.WebApi
{
    /// <summary>
    /// 支付查询
    /// </summary>
    [Route("api/[controller]/[Action]")]
    [ApiController]
    [Authorize(Roles = "admin,q4")]
    public class PaymentController : ControllerBase
    {
        private MyDbContext db;
        public PaymentController(MyDbContext db)
        {
            this.db = db;
        }
        /// <summary>
        /// 获取采购单总金额、已支付金额、未支付金额
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<Count> GetCount() 
        {
            //获取采购单总金额
            double totalMoney = db.PurchaseOrder.Sum(x => x.Money);
            //获取已支付金额
            double money = db.Payments.Sum(x => x.PaymentMoney);
            //计算未支付金额
            double residueMoney = Math.Round(totalMoney - money, 2);

            Count c = new Count()
            {
                TotalMoney = totalMoney,
                Money = money,
                ResidueMoney = residueMoney
            };
            return c;
        }
        /// <summary>
        /// 获取未支付给供应商的数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<List<Unpaid>> GetUserUnpaid()
        {
            //获取供应商总单据金额
            var SupplierTotalSum = db.PurchaseOrder
                .GroupBy(u => u.SupplierName)
                .Select(g => new
                {
                    SupplierName = g.Key,
                    TotalAmount = g.Sum(o => o.Money)
                }).ToList();

            //获取已支付总金额
            var havePaid = db.Payments
                .GroupBy(u => u.Name)
                .Select(g => new
                {
                    Name = g.Key,
                    TotalAmount = g.Sum(o => o.PaymentMoney)
                }).ToList();

            //计算未支付金额
            List<Unpaid> up = new List<Unpaid>();
            foreach (var i in SupplierTotalSum)
            {
                Unpaid u = new Unpaid()
                {
                    Name = i.SupplierName,
                    Money = i.TotalAmount
                };
                foreach (var j in havePaid)
                {
                    if (i.SupplierName == j.Name)
                    {
                        u.Money = Math.Round(i.TotalAmount - j.TotalAmount, 2);
                    }
                }
                if (u.Money == 0)
                {
                    continue;
                }
                up.Add(u);
            }
            return up;
        }
        /// <summary>
        /// 添加付款单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> Add(Payment p)
        {
            db.Payments.Add(p);
            await db.SaveChangesAsync();
            return Ok();
        }
        /// <summary>
        /// 查询最近十条付款单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<Payment>> QueryTen()
        {
            List<Payment> ps = new List<Payment>();
            ps = await db.Payments.OrderByDescending(x => x.Id).Take(10).ToListAsync();
            return ps;
        }
        
    }
    
    /// <summary>
    /// 实体类，供应商名称、未支付金额
    /// </summary>
    public class Unpaid
    {
        public string Name { get; set; }
        public double Money { get; set; }
    }
}
